<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="title">局域网即时通讯系统</h2>
      <el-form :model="loginForm" status-icon :rules="loginRules" ref="loginForm" label-width="0" class="login-form">
        <el-form-item prop="username">
          <el-input 
            prefix-icon="el-icon-user"
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            clearable>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            prefix-icon="el-icon-lock"
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码"
            clearable
            @keyup.enter.native="handleLogin">
          </el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" class="login-button" @click="handleLogin" :loading="loading">登录</el-button>
        </el-form-item>
        
        <div class="register-link">
          <router-link to="/register">还没有账号？立即注册</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { userApi } from '../utils/api'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          this.loading = true
          try {
            const res = await userApi.login(this.loginForm)
            
            // 保存用户信息到Vuex
            this.$store.dispatch('login', {
              id: res.id,
              username: res.username
            })
            
            // 保存信息到localStorage
            localStorage.setItem('userId', res.id)
            localStorage.setItem('username', res.username)
            
            // 初始化WebSocket连接
            this.$initWebSocket(res.id)
            
            // 获取用户列表
            const userList = await userApi.getUserList()
            this.$store.dispatch('updateUserList', userList)
            
            // 获取群聊消息
            const groupMessages = await this.$store.dispatch('getGroupMessages')
            this.$store.commit('SET_GROUP_MESSAGES', groupMessages)
            
            // 跳转到聊天页面
            this.$router.push('/chat')
            
            this.$message.success('登录成功')
          } catch (error) {
            console.error('登录失败:', error)
          } finally {
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7f9;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  margin-bottom: 30px;
  color: #409EFF;
}

.login-form {
  margin-top: 20px;
}

.login-button {
  width: 100%;
}

.register-link {
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
}
</style> 